<template >
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="考试编号" prop="examcode">
        <el-input v-model="dataForm.examcode" placeholder="考试编号"></el-input>
      </el-form-item>
      <el-form-item label="该次考试介绍" prop="description">
        <el-input
          v-model="dataForm.description"
          placeholder="该次考试介绍"
        ></el-input>
      </el-form-item>
      <el-form-item label="课程名称" prop="source">
        <el-input v-model="dataForm.source" placeholder="课程名称"></el-input>
      </el-form-item>
      <el-form-item label="试卷编号" prop="paperid">
        <el-input v-model="dataForm.paperid" placeholder="试卷编号"></el-input>
      </el-form-item>
      <el-form-item label="考试日期" prop="examdate">
        <el-input v-model="dataForm.examdate" placeholder="考试日期"></el-input>
      </el-form-item>
      <el-form-item label="持续时长" prop="totaltime">
        <el-input
          v-model="dataForm.totaltime"
          placeholder="持续时长"
        ></el-input>
      </el-form-item>
      <el-form-item label="年级" prop="grade">
        <el-input v-model="dataForm.grade" placeholder="年级"></el-input>
      </el-form-item>
      <el-form-item label="学期" prop="term">
        <el-input v-model="dataForm.term" placeholder="学期"></el-input>
      </el-form-item>
      <el-form-item label="专业" prop="major">
        <el-input v-model="dataForm.major" placeholder="专业"></el-input>
      </el-form-item>
      <el-form-item label="学院" prop="institute">
        <el-input v-model="dataForm.institute" placeholder="学院"></el-input>
      </el-form-item>
      <el-form-item label="总分" prop="totalscore">
        <el-input v-model="dataForm.totalscore" placeholder="总分"></el-input>
      </el-form-item>
      <el-form-item label="考试类型" prop="type">
        <el-input v-model="dataForm.type" placeholder="考试类型"></el-input>
      </el-form-item>
      <el-form-item label="考生须知" prop="tips">
        <el-input v-model="dataForm.tips" placeholder="考生须知"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        examcode: "",
        description: "",
        source: "",
        paperid: "",
        examdate: "",
        totaltime: "",
        grade: "",
        term: "",
        major: "",
        institute: "",
        totalscore: "",
        type: "",
        tips: "",
      },
      dataRule: {
        examcode: [
          { required: true, message: "考试编号不能为空", trigger: "blur" },
        ],
        description: [
          { required: true, message: "该次考试介绍不能为空", trigger: "blur" },
        ],
        source: [
          { required: true, message: "课程名称不能为空", trigger: "blur" },
        ],
        paperid: [
          { required: true, message: "试卷编号不能为空", trigger: "blur" },
        ],
        examdate: [
          { required: true, message: "考试日期不能为空", trigger: "blur" },
        ],
        totaltime: [
          { required: true, message: "持续时长不能为空", trigger: "blur" },
        ],
        grade: [{ required: true, message: "年级不能为空", trigger: "blur" }],
        term: [{ required: true, message: "学期不能为空", trigger: "blur" }],
        major: [{ required: true, message: "专业不能为空", trigger: "blur" }],
        institute: [
          { required: true, message: "学院不能为空", trigger: "blur" },
        ],
        totalscore: [
          { required: true, message: "总分不能为空", trigger: "blur" },
        ],
        type: [
          { required: true, message: "考试类型不能为空", trigger: "blur" },
        ],
        tips: [
          { required: true, message: "考生须知不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/exam/exammanage/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.examcode = data.examManage.examcode;
              this.dataForm.description = data.examManage.description;
              this.dataForm.source = data.examManage.source;
              this.dataForm.paperid = data.examManage.paperid;
              this.dataForm.examdate = data.examManage.examdate;
              this.dataForm.totaltime = data.examManage.totaltime;
              this.dataForm.grade = data.examManage.grade;
              this.dataForm.term = data.examManage.term;
              this.dataForm.major = data.examManage.major;
              this.dataForm.institute = data.examManage.institute;
              this.dataForm.totalscore = data.examManage.totalscore;
              this.dataForm.type = data.examManage.type;
              this.dataForm.tips = data.examManage.tips;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/exam/exammanage/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              examcode: this.dataForm.examcode,
              description: this.dataForm.description,
              source: this.dataForm.source,
              paperid: this.dataForm.paperid,
              examdate: this.dataForm.examdate,
              totaltime: this.dataForm.totaltime,
              grade: this.dataForm.grade,
              term: this.dataForm.term,
              major: this.dataForm.major,
              institute: this.dataForm.institute,
              totalscore: this.dataForm.totalscore,
              type: this.dataForm.type,
              tips: this.dataForm.tips,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
